#include "Device/MatrixKeybord.h"
#include "Hardware/Timer.h"
#include "Common/BitOpr.h"

#define MatkeyPort P3

char code MatKeyTable[]="0123456789\n\b\045\046\047\050"; //返回的码表,0到9,确定,取消,上下左右
byte code MatKeyMap[]={0xee,0xde,0xbe,0x7e,0xed,0xdd,0xbd,0x7d,0xeb,0xdb,0xbb,0x7b,0xe7,0xd7,0xb7,0x77}; //矩阵键盘的按键映射表

char GetMatKeyPress()
{
	char res;
	uint8 row,line;
	uint8 i;
	
	Delay1us();
	
	MatkeyPort=0xf0; //先找出列
	if( ( MatkeyPort & 0xf0) != 0xf0) //有键被按下
	{
		Delay(20);
		if( ( MatkeyPort & 0xf0) != 0xf0) //判断是否抖动造成的
		{
			row=  MatkeyPort &0xf0; //保存列
			MatkeyPort= row | 0x0f;
			line=  MatkeyPort &0x0f;
			while( ( MatkeyPort & 0x0f) != 0x0f); //检测弹起
			res = line+row;
			goto PRESS;
		}
	}
	
	PortReset(MatkeyPort); //还原IO口的状态
	return 0;
PRESS:
	PortReset(MatkeyPort); //还原IO口的状态
	for(i=0;i<sizeof(MatKeyMap);++i)
	{
		if(MatKeyMap[i]==res)
		{
			return MatKeyTable[i];
		}
	}
	return 0;
}